package com.itboy.mall.pms.brand.controller;

import java.util.List;
import java.util.Arrays;

import com.itboy.mall.pms.brand.domain.MallBrand;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.itboy.common.annotation.RepeatSubmit;
import com.itboy.common.annotation.Log;
import com.itboy.common.core.controller.BaseController;
import com.itboy.common.core.domain.AjaxResult;
import com.itboy.common.enums.BusinessType;
import com.itboy.mall.pms.brand.vo.MallBrandVo;
import com.itboy.mall.pms.brand.bo.MallBrandQueryBo;
import com.itboy.mall.pms.brand.bo.MallBrandAddBo;
import com.itboy.mall.pms.brand.bo.MallBrandEditBo;
import com.itboy.mall.pms.brand.service.IMallBrandService;
import com.itboy.common.utils.poi.ExcelUtil;
import com.itboy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 品牌Controller
 *
 * @author itboy
 * @date 2021-07-13
 */
@Api(value = "品牌控制器", tags = {"品牌管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mall/brand")
public class MallBrandController extends BaseController {

    private final IMallBrandService iMallBrandService;

    /**
     * 查询品牌列表
     */
    @ApiOperation("查询品牌列表")
    @PreAuthorize("@ss.hasPermi('mall:brand:list')")
    @GetMapping("/list")
    public TableDataInfo<MallBrandVo> list(@Validated MallBrandQueryBo bo) {
        return iMallBrandService.queryPageList(bo);
    }

	/**
	 * 查询品牌列表
	 */
	@ApiOperation("查询品牌列表")
	@GetMapping("/queryList")
	public AjaxResult<List<MallBrand>> queryList() {
		return AjaxResult.success(iMallBrandService.list());
	}

    /**
     * 导出品牌列表
     */
    @ApiOperation("导出品牌列表")
    @PreAuthorize("@ss.hasPermi('mall:brand:export')")
    @Log(title = "品牌", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MallBrandVo> export(@Validated MallBrandQueryBo bo) {
        List<MallBrandVo> list = iMallBrandService.queryList(bo);
        ExcelUtil<MallBrandVo> util = new ExcelUtil<MallBrandVo>(MallBrandVo.class);
        return util.exportExcel(list, "品牌");
    }

    /**
     * 获取品牌详细信息
     */
    @ApiOperation("获取品牌详细信息")
    @PreAuthorize("@ss.hasPermi('mall:brand:query')")
    @GetMapping("/{brandId}")
    public AjaxResult<MallBrandVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("brandId") String brandId) {
        return AjaxResult.success(iMallBrandService.queryById(brandId));
    }

    /**
     * 新增品牌
     */
    @ApiOperation("新增品牌")
    @PreAuthorize("@ss.hasPermi('mall:brand:add')")
    @Log(title = "品牌", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MallBrandAddBo bo) {
        return toAjax(iMallBrandService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改品牌
     */
    @ApiOperation("修改品牌")
    @PreAuthorize("@ss.hasPermi('mall:brand:edit')")
    @Log(title = "品牌", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MallBrandEditBo bo) {
        return toAjax(iMallBrandService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除品牌
     */
    @ApiOperation("删除品牌")
    @PreAuthorize("@ss.hasPermi('mall:brand:remove')")
    @Log(title = "品牌" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{brandIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable String[] brandIds) {
        return toAjax(iMallBrandService.deleteWithValidByIds(Arrays.asList(brandIds), true) ? 1 : 0);
    }
}
